<?php /* COMPANIES $Id: addedit.php,v 1.49.4.2 2005/11/07 04:44:49 pedroix Exp $ */
$company_id = intval( dPgetParam( $_GET, 'company_id', 0 ) );
// trangdt add
$company_parent = intval( dPgetParam( $_GET, 'company_parent', 0 ) );

// check permissions for this company
$perms =& $AppUI->acl();
// If the company exists we need edit permission,
// If it is a new company we need add permission on the module.
if ($company_id)
	$canEdit = $perms->checkModuleItem($m, 'edit', $company_id);
else
	$canEdit = $perms->checkModule($m, 'add');

if (!$canEdit) {
	$AppUI->setMsg('Access denied', UI_MSG_ERROR);
	$AppUI->redirect();
}

// load the company types
$types = dPgetSysVal( 'CompanyType' );

// load the record data
$q  = new DBQuery;
$q->addTable('companies');
$q->addQuery('companies.*');
//$q->addQuery('con.contact_first_name');
//$q->addQuery('con.contact_last_name');
//$q->addJoin('users', 'u', 'u.user_id = companies.company_owner');
//$q->addJoin('contacts', 'con', 'u.user_contact = con.contact_id');
$q->addWhere('companies.company_id = '.$company_id);
$sql = $q->prepare();
$q->clear();

$obj = null;
if (!db_loadObject( $sql, $obj ) && $company_id > 0) {
	// $AppUI->setMsg( '	$qid =& $q->exec(); Company' ); // What is this for?
	$AppUI->setMsg( 'invalidID', UI_MSG_ERROR, true );
	$AppUI->redirect();
}

// collect all the users for the company owner list
$q  = new DBQuery;
$q->addTable('users','u');
$q->addTable('contacts','con');
$q->addQuery('user_id');
$q->addQuery('CONCAT_WS(" ",contact_first_name,contact_last_name)');
$q->addOrder('contact_first_name');
$q->addWhere('u.user_contact = con.contact_id');
$q->addWhere('u.user_id <> 1');
$owners = $q->loadHashList();
// trangdt add
// load parent Name
$parent_name='';
if($company_parent != 0){
	$q->addTable('companies');
	$q->addQuery('company_name');
	$q->addWhere('company_id='.$company_parent);
	$parent_name = $q->loadResult();
}
require_once(DP_BASE_DIR."/modules/masterdata/mdstatus.class.php");
$status_list = getStatusList('Customer');

require_once(DP_BASE_DIR."/modules/masterdata/mdtype.class.php");
$type_list = getTypeList('Customer');

require_once(DP_BASE_DIR."/modules/masterdata/classification/classification.class.php");
$cls_list = arrayMerge(array( 0 => '' ), getClassificationList('Customer'));

require_once(DP_BASE_DIR."/modules/masterdata/mddivision.class.php");
$division_list = arrayMerge(array( 0 => '' ),getDivisionList());

require_once(DP_BASE_DIR."/modules/masterdata/mdcostcenter.class.php");
$costcenter_list = arrayMerge(array( 0 => '' ),getCostCenterList());
$costcenter_name='';
// get cost center name;
if($obj->company_costcenter >0){
	$cc = new CMdCostCenter();
	$cc->load($obj->company_costcenter);
	$costcenter_name = $cc->cc_name;
}

require_once( $AppUI->getModuleClass( 'admin' ) );
$userO = new CUser();

$q = new DBQuery();
$q->addTable('users', 'u');
$q->addTable('contacts', 'c');
//$q->addTable('project_contacts', 'pc');
$q->addQuery('user_id, concat(contact_first_name, \' \', contact_last_name) as contact_name');
//$q->addWhere('pc.contact_id = u.user_id AND u.user_contact = c.contact_id AND pc.project_id = '.$task_project);
$q->addWhere('u.user_contact = c.contact_id');
$q->addOrder('contact_first_name');
$sql = $q->prepare();
$resource_list = arrayMerge(array( '' => '' ), db_loadHashList($sql));
// trangdt end
// setup the title block
$ttl = $company_id > 0 ? 'Edit Customer' : 'Add Customer';
$titleBlock = new CTitleBlock( $ttl );
$titleBlock->addButton( 'company list', 'index.php?m=companies' );
$titleBlock->show();

$q = new DBQuery();
$q->addTable('companies');
$q->addQuery('company_code');
if (isset($obj->company_code)) {
	$q->addWhere("company_id != $obj->company_id");
}
$quoteCodeList = $q->loadColumn();
$qCL = '["' . implode('", "', $quoteCodeList) . '"]';
?>

<script language="javascript">
function submitIt() {
	/*
	var form = document.changeclient;
	if (form.company_name.value.length < 3) {
		alert( '<?=$AppUI->_('companyValidName', UI_OUTPUT_JS)?>' );
		form.company_name.focus();
	} else {
		form.submit();
	}*/
}

function testURL( x ) {
	var test = document.changeclient.company_primary_url.value;
	if (test.length > 6) {
		newwin = window.open( 'http://' + test, 'newwin', '' );
	}
}
</script>
<div id="editComPage">
<form name="changeclient" action="?m=companies" method="post">
	<input type="hidden" name="dosql" value="do_company_aed" />
	<input type="hidden" name="company_id" value="<?=$company_id?>" />
	<input type="hidden" name="company_parent" value="<?=$company_parent?>" />
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr><td class="tabon">Information</td><td width="100%"></td></tr>
</table>
<table border="0" cellpadding="7" cellspacing="0" width="100%" class="infopanel">
<tr>
	<td><input type="submit" value="<?=$AppUI->_('Save')?>" class="button" /></td>
	<td><input type="button" value="<?=$AppUI->_('Cancel')?>" class="button" onClick="history.back(-1)"/></td>
	<td width="100%">&nbsp;</td>
<? if ($company_id != 0) { ?>
	<td><?=showButton($AppUI->_('View'), '?m=companies&a=view&company_id='.$company_id)?></td>
<? } ?>
<tr>
</table>
<table cellspacing="0" cellpadding="7" border="0" width='100%' class="subinfopanel">
<tr>
<td>
	<table>
	<?if($company_parent !=0){?>
	<tr>
		<td align="right"><?=$AppUI->_('Parent Customer Name')?></td>
		<td colspan="4"><div class="text" style="width: 250px"><a href="javascript:Company(<?=$company_parent?>).show()"><b><?=$parent_name?></b></a></div></td>
	</tr>
	<?}?>
	<tr>
		<td align="right">* <?=$AppUI->_('Customer code')?></td>
		<td colspan="100%">
			<input type="text" class="text" name="company_code" id="company_code" title="Company Code :: required" value="<?=dPformSafe(@$obj->company_code);?>"
				style="width: 325px" maxlength="20"/>
		</td>
	</tr>
	<tr>
		<td align="right">* <?=$AppUI->_('Customer Name')?></td>
		<td colspan="100%">
			<input type="text" class="text" name="company_name" id="company_name" title="Company Name :: required" value="<?=dPformSafe(@$obj->company_name);?>"
				style="width: 325px" maxlength="150"/>
		</td>
	</tr>
	<tr>
		<td align="right"><?=$AppUI->_('Exact Code')?></td>
		<td colspan=>
			<input type="text" class="text" name="company_exact_code"
				value="<?=@$obj->company_exact_code?>" maxlength="255" style="width: 100px"/>
		</td>
		<td align="right"><?=$AppUI->_('VAT Code')?></td>
		<td colspan="4">
			<input type="text" class="text" name="company_vat_code"
				value="<?=@$obj->company_vat_code?>" maxlength="255" style="width: 100px"/>
		</td>
	</tr>
	<tr>
		<td align="right">* <?=$AppUI->_('Ledger 1')?></td>
		<td>
			<input type="text" class="text" name="company_ledge1" id="company_ledge1" title="Ledge1:: required"
				value="<?=@$obj->company_ledge1?>" maxlength="20" style="width: 100px"/>
		</td>
		<td align="right" width="50px">* <?=$AppUI->_('Ledger 2')?></td>
		<td colspan="3">
			<input type="text" class="text" name="company_ledge2" id="company_ledge2" title="Ledge1:: required"
				value="<?=@$obj->company_ledge2?>" maxlength="20" style="width: 100px"/>
		</td>
	</tr>
	<tr>
		<td align="right"><?=$AppUI->_('Markup')?></td>
		<td>
			<input type="text" class="text" id="company_markup" name="company_markup"
				value="<?=@$obj>company_markup?>" maxlength="4" style="width: 50px"/>%
		</td>
		<td align="right"><?=$AppUI->_('Fax')?></td>
		<td>
			<input type="text" class="text" name="company_fax" value="<?=dPformSafe(@$obj->company_fax)?>"
				style="width: 100px" maxlength="30"/>
		</td>
	</tr>
	<!--
	<tr>
		<td align="right">Website http://<A name="x"></a></td>
		<td colspan="3">
			<input type="text" class="text" name="company_primary_url" value="<?=dPformSafe(@$obj->company_primary_url)?>"
				style="width: 325px" maxlength="255"/>
		</td>
		<td>
			<a href="#x" onClick="testURL('CustomerURLOne')">[<?=$AppUI->_('test')?>]</a>
		</td>
	</tr>
	<tr>
		<td align="right"><?=$AppUI->_('Email')?></td>
		<td colspan="3">
			<input type="text" class="text" name="company_email" id="company_email" value="<?=dPformSafe(@$obj->company_email)?>"
				style="width: 325px" maxlength="255"/>
		</td>
	</tr>
	-->
	<tr>
		<td align="right"><?=$AppUI->_('Phone')?></td>
		<td>
			<input type="text" class="text" name="company_phone1" value="<?=dPformSafe(@$obj->company_phone1)?>"
				style="width: 110px" maxlength="30"/>
		</td>
		<td align="center"><?=$AppUI->_('or')?></td>
		<td>
			<input type="text" class="text" name="company_phone2" value="<?=dPformSafe(@$obj->company_phone2)?>"
				style="width: 100px" maxlength="30"/>
		</td>
	</tr>
	<tr>
		<td align="right" valign="top"><?=$AppUI->_('Address')?></td>
		<td colspan="4">
			<input type="text" class="text" name="company_address1" value="<?=dPformSafe(@$obj->company_address1)?>"
				style="width: 325px" maxlength="150"/>
		</td>
	</tr>
	<tr style="display: none">
		<td colspan="6"><input type="hidden" name="company_address2" value="<?=dPformSafe(@$obj->company_address2)?>" size=50 maxlength="255" /></td>
	</tr>
	<!--
	<tr>
		<td align="right"><?=$AppUI->_('City')?></td>
		<td>
			<input type="text" class="text" name="company_city" value="<?=dPformSafe(@$obj->company_city)?>"
				style="width: 110px" maxlength="50" /></td>
		<td align="right"><?=$AppUI->_('State')?></td>
		<td><input type="text" class="text" name="company_state" value="<?=dPformSafe(@$obj->company_state)?>"
				style="width: 110px" maxlength="50" /></td>
		<td align="right"><?=$AppUI->_('Zip')?></td>
		<td><input type="text" class="text" name="company_zip" value="<?=dPformSafe(@$obj->company_zip)?>"
				style="width: 100px" maxlength="15"/></td>
	</tr>
	-->
	<!--
<? if ($AppUI->user_id == 1) { ?>
	<tr>
		<td align="right"><?=$AppUI->_('Customer Owner')?></td>
		<td>
	<?php
		echo arraySelect( $owners, 'company_owner', 'size="1" class="text"', @$obj->company_owner ? $obj->company_owner : $AppUI->user_id );
	?>
		</td>
	</tr>
<? } else { ?>
	<input type="hidden" name="company_owner" value="<?=@$obj->company_owner ? $obj->company_owner : $AppUI->user_id?>"/>
<? } ?>
-->
	<? // trandt add?>
	<tr>
		<td align="right">* <?=$AppUI->_('Status')?></td>
		<td>

			<? echo arraySelect($status_list,'company_status','id="company_status" title="Status :: required" size="1" class="text" style="width: 100px"',@$obj->company_status)?>
		</td>
		<td align="right">* <?=$AppUI->_('Type')?></td>
		<td>
			<? echo arraySelect($type_list,'company_type2','id="company_type2" title="Type:: required" size="1" class="text" style="width: 100px"',@$obj->company_type2)?>
		</td>
	</tr>
	<!-- Divisions -->
	<tr>
		<td align="right"><?=$AppUI->_('Division')?></td>
		<td>
			<? /*echo arraySelect($division_list,'company_division',
								'id="idSupDivisionId" size="1" class="text" style="width: 285px" onchange="doChangeDivision()"',
								@$row->company_division)*/
				echo arraySelect($division_list,'company_division','size="1" class="text" style="width: 100px"',@$obj->company_division)
			?>
		</td>
		<!-- costcenter -->
		<td align="right" width="80px"><?=$AppUI->_('Cost Center')?></td>
		<td colspan="3" align="left" nowrap="nowrap">
			<input id="idComCostCenterName" type="text" class="text" name="company_costcenter_name"
				value="<?=$costcenter_name?>" maxlength="100" style="width: 150px" readonly="true"/>
			<img class="ico clickable" src="images/treetable.gif" onclick="doShowCostCenterSelector()"/>
			<input id="idComCostCenterId" type='hidden' name='company_costcenter' value='<?=@$obj->company_costcenter?>'>
		</td>
	</tr>
	<tr>
		<td align="right"><?=$AppUI->_('Resource')?></td>
		<td>
			<? echo arraySelect($resource_list,'company_resource','',@$obj->company_resource)?>
		</td>
		<td align="right"><?=$AppUI->_('Classification')?></td>
		<td>
			<? echo arraySelect($cls_list,'company_classification','size="1" class="text" style="width: 100px"',@$obj->company_classification)?>
		</td>
	</tr>
	<?// trandt end?>
	<tr>
		<td align="right" valign=top><?=$AppUI->_('Description')?></td>
		<td colspan="4" align="left">
		<textarea style="width: 420px" rows="6" class="textarea" name="company_description"><?=@$obj->company_description?></textarea>
		</td>
	</tr>
</table>
</td>
	<td align='left'>
		<?php
 			require_once('./classes/CustomFields.class.php');
 			$custom_fields = new CustomFields( $m, $a, $obj->company_id, 'edit' );
 			$custom_fields->printHTML();
		?>
	</td>
</tr>
</table>
</form>
</div>
<div id="validation_rules" style="visibility: hidden; display: none">
	var fcompany_code = new LiveValidation('company_code');
	fcompany_code.add( Validate.Presence);
	fcompany_code.add( Validate.Length, { maximum: 120 } );
	fcompany_code.add( Validate.Exclusion , {within: <?=$qCL?>});

	var fcompany_name = new LiveValidation('company_name');
	fcompany_name.add( Validate.Presence);
	fcompany_name.add( Validate.Length, { maximum: 150 } );

	var fcompany_type = new LiveValidation('company_type2');
	fcompany_type.add( Validate.Presence);
	fcompany_type.add( Validate.Length, {minimum: 1, maximum: 9});

	var fcompany_status = new LiveValidation('company_status');
	fcompany_status.add( Validate.Presence);
	fcompany_status.add( Validate.Length, {minimum: 1, maximum: 9});

	var fcompany_ledge1 = new LiveValidation('company_ledge1');
	fcompany_ledge1.add( Validate.Presence);
	fcompany_ledge1.add( Validate.Length, { maximum: 20 } );

	var fcompany_ledge2 = new LiveValidation('company_ledge2');
	fcompany_ledge2.add( Validate.Presence);
	fcompany_ledge2.add( Validate.Length, { maximum: 20 } );

	var fcompany_markup = new LiveValidation('company_markup');
	fcompany_markup.add( Validate.Numericality, {minimum: 1, maximum: 100} );

	//var fcompany_email = new LiveValidation('company_email');
	//fcompany_email.add( Validate.Email);

	new Tips($$('.text'));
</div>
<script type="text/javascript" language="javascript">
	eval($('validation_rules').innerHTML);
</script>